/*
  ivl_add.c  -  implementation of addition functions

  This file is part of the IVL Library (LIBIVL).
  
  LIBIVL - library for interval computations 
  
  Copyright (c) 2007 Mateusz Paprocki
  
  The IVL Library is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  The IVL Library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with the IVL Library; see the file LICENCE.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "interval.h"

int ivl_add(ivl_ptr ivl, ivl_srcptr x, ivl_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x) || DOM_UNDEFINED(y))
		ivl->domain = IVL_UNDEFINED;
	else {	
		inexact_left = mpfr_add(IVL_LEFT(ivl), IVL_LEFT(x), IVL_LEFT(y), GMP_RNDD);
		inexact_right = mpfr_add(IVL_RIGHT(ivl), IVL_RIGHT(x), IVL_RIGHT(y), GMP_RNDU);
	
		IVL_CHECK(ivl, x->domain | y->domain, inexact, inexact_left, inexact_right);				
	}
	
	return inexact;
}

int ivl_add_u(ivl_ptr ivl, ivl_srcptr x, unsigned long y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_add_ui(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_add_ui(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_add_s(ivl_ptr ivl, ivl_srcptr x, long y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_add_si(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_add_si(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_add_d(ivl_ptr ivl, ivl_srcptr x, double y) {
	int inexact = 0, inexact_left, inexact_right;
	mpfr_t tmp;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		mpfr_init2(tmp, 53);
		
		inexact_left = mpfr_set_d(tmp, y, GMP_RNDD);
		inexact_left |= mpfr_add(IVL_LEFT(ivl), IVL_LEFT(x), tmp, GMP_RNDD);
		
		inexact_right = mpfr_set_d(tmp, y, GMP_RNDU);
		inexact_right |= mpfr_add(IVL_RIGHT(ivl), IVL_RIGHT(x), tmp, GMP_RNDU);
		
		mpfr_clear(tmp);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_add_z(ivl_ptr ivl, ivl_srcptr x, mpz_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_add_z(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_add_z(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_add_q(ivl_ptr ivl, ivl_srcptr x, mpq_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_add_q(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_add_q(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_add_f(ivl_ptr ivl, ivl_srcptr x, mpfr_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {		
		inexact_left = mpfr_add(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_add(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

